<?php

namespace App\Exports;

use App\Enums\ProductType;
use App\Models\Download;
use App\Models\Product;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ProductExport implements FromQuery, WithMapping, WithHeadings
{
    public $data;
    public $request;

    public function __construct(Download $data)
    {
        $this->data = $data;
        $this->request = json_decode($this->data->request, true);
    }

    public function query()
    {
        $query = Product::query()
            ->when(!empty($this->request['keyword']), function ($q) {
                $likeVal = '%' . $this->request['keyword'] . '%';

                return $q->where('name', 'like', $likeVal);
            })
            ->withCount(['available_tokens'])
            ->when(!empty($this->request['start_date']), function ($q) {
                $q->whereDate('created_at', '>=', $this->request['start_date']);
            })
            ->when(!empty($this->request['end_date']), function ($q) {
                $q->whereDate('created_at', '<=', $this->request['end_date']);
            })
            ->when(0 == $this->data->agent_id, function ($q) {
                return $q->with('agent');
            });

        if (empty($this->request['type'])) {
            $query = $query->where('type', ProductType::ISSUE);
        } elseif ('blind' == $this->request['type']) {
            $query = $query->where('type', ProductType::BLIND_BOX);
        } elseif ('resale' == $this->request['type']) {
            $query = $query->where('type', ProductType::RESALE);
        }

        if (!empty($this->request['agent_id'])) {
            $query = $query->where('agent_id', $this->request['agent_id']);
        }

        if (!empty($this->data->agent_id)) {
            $query = $query->where('agent_id', $this->data->agent_id);
        }

        return $query->orderByDesc('id')
            ->limit(30000);
    }

    public function map($value): array // 指定excel中每一列的数据字段
    {
        $map = [
            $value['id'],
            $value['name'],
            $value['left_count'] ?? 0,
            $value['available_tokens_count'] ?? 0,
            $value['sales_count'] ?? 0,
            'on_sale' == $value['status']->value ? '上架' : '下架',
            $value['created_at'],
        ];

        if (0 == $this->data->agent_id) {
            $map = [
                $value['id'],
                $value['name'],
                $value['contract_address'],
                !empty($value->agent) ? $value->agent->name : '-',
                $value['left_count'] ?? 0,
                $value['available_tokens_count'] ?? 0,
                $value['sales_count'] ?? 0,
                'on_sale' == $value['status']->value ? '上架' : '下架',
                $value['created_at'],
            ];
        }

        return $map;
    }

    public function headings(): array // 指定excel的表头
    {
        $heading = [
            'ID',
            '商品名称',
            '发行库存',
            '可用库存',
            '销售量',
            '状态',
            '添加时间',
        ];
        if (0 == $this->data->agent_id) {
            $heading = [
                'ID',
                '商品名称',
                '商品编号',
                '代理商',
                '发行库存',
                '可用库存',
                '销售量',
                '状态',
                '添加时间',
            ];
        }

        return $heading;
    }
}
